package com.stone.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.stone.model.Admin;
import com.stone.model.Provider;
import com.stone.service.IAdminService;
import com.stone.utils.EncryptUtil;
import com.stone.utils.Page;

public class AdminAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5003003575318308214L;

	private String userName = "";
	private String password = "";
	private String oldPassword;
	private Long adminId;
	private Admin admin;
	private boolean flag;
	private String checkflag ; // add  or update!!
	private String error;
	private int existFlag; 
	private ArrayList<Admin> adminList;

	private IAdminService adminService;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public IAdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}
	
	
	//------------------------------------------------
	private static Logger logger = Logger.getLogger("系统登录信息");
	//----------------------------------------------------------------
	public String login(){
		boolean flag = adminService.check(userName, EncryptUtil.md5(password));
		if(flag){
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("admin", adminService.findAdminByName(userName));

			logger.fatal(userName + "登录到系统...");

			userName = "";
			password = "";
			return SUCCESS;
		}else{
			error = ">>>用户名或密码错误，请重新登录!";
			userName = "";
			password = "";
			return ERROR;
		}
	}
	
	public String logout(){
		userName = "";
		password = "";
		Map<String, Object> session = ActionContext.getContext().getSession();
		logger.fatal(((Admin)session.get("admin")).getUsername()+"退出系统...");
		session.remove("admin");
		session.clear();
		return SUCCESS;
	}
	
	public String toLogin(){
		userName = "";
		password = "";
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.remove("admin");		
		return "login";
	}
	
	public String changePassword(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		Admin admin = (Admin) session.get("admin");
		if(EncryptUtil.md5(oldPassword).equals(admin.getPassword())){
			admin.setPassword(EncryptUtil.md5(password));
			adminService.updateAdmin(admin);
			session.put("admin", adminService.findAdminByName(userName));
			flag = true;
		}else{
			flag = false;
		}
		password = "";
		return SUCCESS;
	}
	
	public String findAllAdministrators()
	{
		adminList = new ArrayList<Admin>();
		adminList = (ArrayList<Admin>) adminService.findAllAdministrators();
	
		return "success";
		
	}
	
	public String saveAdministrator()
	{
		admin.setPassword(EncryptUtil.md5(admin.getPassword()));
		adminService.addAdministrator(admin);
		return "success";
	}
	
	public String findAdministrator()
	{
		admin = adminService.findAdminById(adminId);
		return "success";
	}
	
	public String checkNameExist(){
		
		if(adminService.findAdminByName(userName)!=null)
		{
			existFlag = 1;
			if(checkflag.equals("update")){
				Admin a = adminService.findAdminByName(userName);
				if(a.getId().compareTo(adminId) == 0)
					existFlag--;
				}
		}
		else
			existFlag = 0;
		
		return SUCCESS;
	}
	
	public String updateAdministrator()
	{
		Admin a = adminService.findAdminById(adminId);
		if(!admin.getPassword().equals(""))
			a.setPassword(EncryptUtil.md5(admin.getPassword()));
		if(!admin.getUsername().equals(""))
			a.setUsername(admin.getUsername());
		adminService.updateAdmin(a);
		return "success";
	}
	
	public String deleteAdministrator()
	{
		adminService.deleteAdministrator(adminService.findAdminById(adminId));
		return "success";
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}
	
	public ArrayList<Admin> getAdminList() {
		return adminList;
	}

	public void setAdminList(ArrayList<Admin> adminList) {
		this.adminList = adminList;
	}

	public Long getAdminId() {
		return adminId;
	}

	public void setAdminId(Long adminId) {
		this.adminId = adminId;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public int getExistFlag() {
		return existFlag;
	}

	public void setExistFlag(int existFlag) {
		this.existFlag = existFlag;
	}

	public String getCheckflag() {
		return checkflag;
	}

	public void setCheckflag(String checkflag) {
		this.checkflag = checkflag;
	}
	
	
}
